import jwt
from util.log import Log

class Token:
    def __init__(self):
        # private
        self.__SECRET_KEY = 'your-256-bit-secret'

    def generateToken(self, payload: dict) -> str:
        try:
            token = jwt.encode(payload, self.__SECRET_KEY, algorithm='HS256')
            return token
        except Exception as e:
            Log.error('生成 Token 异常', e)
            return None
    
    def verify_token(self, token) -> bool:
        try:
            # 如果 token 过期或签名不正确，将会抛出异常
            decoded_payload = jwt.decode(token, self.__SECRET_KEY, algorithms=['HS256'])
            return decoded_payload
        except Exception as e:
            Log.error('校验 Token 异常', e)
            return None

class MusicToken:
    def __init__(self):
        # private
        self.__SECRET_KEY = 'xi/*haha_[]123-^AcP'

    def generateToken(self, payload: dict) -> str:
        try:
            token = jwt.encode(payload, self.__SECRET_KEY, algorithm='HS256')
            return token
        except Exception as e:
            Log.error('生成 Token 异常', e)
            return None
    
    def verify_token(self, token) -> dict:
        try:
            # 如果 token 过期或签名不正确，将会抛出异常
            decoded_payload = jwt.decode(token, self.__SECRET_KEY, algorithms=['HS256'])
            return decoded_payload
        except Exception as e:
            Log.error('校验 Token 异常', e)
            return None

